﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using TST.SharePoint.Shared;

namespace TST.WebParts
{
    internal class PageColumnFilterEditor : EditorPart
    {
        private DropDownList _selectField;

        public PageColumnFilterEditor(string webPartID)
        {
            this.ID = string.Format("SelectListFieldEditor_{0}", webPartID);
            this.Title = "Page Column Filter options";
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            HtmlTable table = new HtmlTable();
            table.CellPadding = 5;
            table.CellSpacing = 3;
            Controls.Add(table);

            HtmlTableRow rowField = new HtmlTableRow();
            table.Controls.Add(rowField);
            HtmlTableCell cellField = new HtmlTableCell();
            rowField.Controls.Add(cellField);

            if (DatasourceContentType.IsWSS)
            {
                Label description = new Label();
                description.Text = "This filter web part can only be used in publishing sites of Microsoft Office SharePoint Server.";
                cellField.Controls.Add(description);
                return;
            }

            _selectField = new DropDownList();
            _selectField.Items.Add(
                new ListItem("-- Please select a field --",
                Guid.Empty.ToString("B")));

            if (SPContext.Current.List != null)
            {
                Label description = new Label();
                description.Text = string.Format("<strong>{0}</strong>: select a field", SPContext.Current.List.Title);
                cellField.Controls.Add(description);

                cellField.Controls.Add(new LiteralControl("<br>"));

                List<KeyValuePair<Guid, string>> items;
                items = new List<KeyValuePair<Guid,string>>();
                for (int i = 0; i < SPContext.Current.List.Fields.Count; i++)
                {
                    SPField field = SPContext.Current.List.Fields[i];
                    if (!field.Hidden)
                    {
                        items.Add(new KeyValuePair<Guid, string>
                            (field.Id, field.Title));
                    }
                }
                items.Sort(
                    delegate(
                        KeyValuePair<Guid, string> fields1, 
                        KeyValuePair<Guid, string> fields2)
                    {
                    return fields1.Value.CompareTo(fields2.Value);
                    }
                );
                for (int i = 0; i < items.Count; i++)
                {
                    if (items[i].Key != Guid.Empty)
                    {
                        _selectField.Items.Add(new ListItem(
                            items[i].Value, 
                            items[i].Key.ToString("B")));
                    }
                }
            }
            cellField.Controls.Add(_selectField);

            HtmlTableRow rowHelp = new HtmlTableRow();
            table.Controls.Add(rowHelp);
            HtmlTableCell cellHelp = new HtmlTableCell();
            rowHelp.Controls.Add(cellHelp);

            Label helpDescription = new Label();
            helpDescription.Text = "Select one of the fields of the Pages library in the list above. The value of this field for the page containing this web part will be sent as the filter value.";
            cellHelp.Controls.Add(helpDescription);
        }

        public override bool ApplyChanges()
        {
            if (_selectField != null)
            {
                PageColumnFilter webpart = WebPartToEdit as PageColumnFilter;
                webpart.PageFieldID = new Guid(_selectField.SelectedValue);
            }
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            if (_selectField != null)
            {
                PageColumnFilter webpart = WebPartToEdit as PageColumnFilter;
                _selectField.SelectedValue = webpart.PageFieldID.ToString("B");
            }
        }
    }
}
